function K_NS()
{
	function K_TreeCascadingSelect(div,input,data,forceTail,tips)
	{
		this.div=typeof(div=="string")?document.getElementById(div):div;
		this.input=typeof(input=="string")?document.getElementById(input):input;
		this.data=data;
		this.tips=tips;
		this.startIndex=forceTail?0:1;
		//clear container
		while(this.div.firstChild){this.div.removeChild(this.div.firstChild);}
		this.showSelect(data.getRootItem(),0);
		if(this.input)
		{
			this.setValue(this.input.value);
		}
	}
	//show select
	K_TreeCascadingSelect.prototype.showSelect=function(item,level)
	{
		if(!item.getFirstChild()){return;}
		var select=this.div.childNodes[level];
		if(!select)
		{
			select=document.createElement("select");
			select.className="K_linkage";
			this.div.appendChild(select);
		}
		select.options.length=0;
		if(this.startIndex)
		{
			var option=document.createElement("option");
			option.text=(this.tips && this.tips.length>level)?this.tips[level]:"Please select";
			select.options.add(option);
		}
		K_Global.bind(select,"change",this,this.onChangeCallback(level));
		for(var child=item.getFirstChild();child;child=child.getNextSibling())
		{
			select.options.add(this.createOption(child));
		}
		while(select.nextSibling)
		{
			this.div.removeChild(select.nextSibling);
		}
		this.onChange(level);
	}
	//create select Option
	K_TreeCascadingSelect.prototype.createOption=function(item)
	{
		var option=document.createElement("option");
		option.text=item.getProperty("text");
		option.value=item.getProperty("id");
		return option;
	}
	//update next select when selech changed
	K_TreeCascadingSelect.prototype.onChangeCallback=function(level)
	{
		return function()
		{
			this.onChange(level);
		}
	}
	//update select
	K_TreeCascadingSelect.prototype.onChange=function(level)
	{
		var select=this.div.childNodes[level];
		var value=select.selectedIndex;
		
		if(value==0 && this.startIndex)
		{//如果切换为不选择状态，则移除后面的select
			while(select.nextSibling)
			{
				this.div.removeChild(select.nextSibling);
			}
		}
		else
		{//否则显示下一级的选择框
			var item=this.data.getRootItem();
			for(var i=0;i<=level;i++)
			{
				item=item.getChildByIndex(this.div.childNodes[i].selectedIndex-this.startIndex);
			}
			if(!item.getFirstChild())//如果当前的选项没有子选项,则移除后面的select
			{
				while(select.nextSibling)
				{
					this.div.removeChild(select.nextSibling);
				}
			}
			else
			{
				this.showSelect(item,level+1);
			}
		}
		this.update();
	}
	K_TreeCascadingSelect.prototype.update=function()
	{
		this.input.value=this.getValue();
	}
	//返回当前选择的值，以逗号分割
	K_TreeCascadingSelect.prototype.getValue=function()
	{
		var arr=[];
		for(var select=this.div.firstChild;select;select=select.nextSibling)
		{
			if(select.selectedIndex==0 && this.startIndex){break;}
			var value=select.options[select.selectedIndex].value;
			if(value)
			{
				arr.push(value);
			}
		}
		return arr.join(",");
	}
	//设置选单的值,这个方法复杂到用setValueBase进行递归是要考虑到没有ID的节点的情况
	K_TreeCascadingSelect.prototype.setValue=function(value)
	{
		if(!value){return;}
		var arr=value.split(',');
		this.setValueBase(arr,0,0);
	}
	K_TreeCascadingSelect.prototype.setValueBase=function(arr,sIndex,vIndex)
	{
		if(vIndex>=arr.length){return;}
		var select=this.div.childNodes[sIndex];
		if(!select){return;}
		for(var i=this.startIndex;i<select.options.length;i++)
		{
			if(select.options[i].value==arr[vIndex])
			{
				select.selectedIndex=i;
				this.onChange(sIndex);
				this.setValueBase(arr,sIndex+1,vIndex+1);
				return true;
			}
		}
		for(var i=this.startIndex;i<select.options.length;i++)
		{
			select.selectedIndex=i;
			this.onChange(sIndex);
			if(this.setValueBase(arr,sIndex+1,vIndex))
			{
				return true;
			}
		}
	}
	window.K_TreeCascadingSelect=K_TreeCascadingSelect;
}
K_NS();